package com.yanqu.road.dao.impl.activity.twins;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.twins.CrossTwinsLuckDrawData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class CrossTwinsDrawDataDaoImpl extends TempDataObjectDao<CrossTwinsLuckDrawData> {

    public static String getDataSql = "select * from t_c_activity_twins_draw where activityId=? and groupId=?";
    public CrossTwinsLuckDrawData getData(int activityId,int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTwinsLuckDrawData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertDataSql = "insert into t_c_activity_twins_draw(" +
            " activityId,groupId,drawTimes,luckCount) " +
            " values (?,?,?,?)";
    @Override
    protected boolean addData(CrossTwinsLuckDrawData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLuckCount()));

        if (getDb().executeNoneQuery(insertDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDataSql = "update t_c_activity_twins_draw set " +
            " drawTimes=?,luckCount=?" +
            " where activityId=? and groupId=?";
    @Override
    protected boolean updateData(CrossTwinsLuckDrawData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLuckCount()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossTwinsLuckDrawData parseData(ResultSet rs) throws SQLException {
        CrossTwinsLuckDrawData data = new CrossTwinsLuckDrawData();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setDrawTimes(rs.getInt("drawTimes"));
        data.setLuckCount(rs.getInt("luckCount"));

        return data;
    }

}
